class WishList < ActiveRecord::Base
  belongs_to :user
  has_many :materials_wish_lists, :dependent => :destroy
  has_many :materials, :through => :materials_wish_lists

  validates_presence_of :user_id, :name, :type_list
  validates_uniqueness_of :name, :scope => :user_id

  TYPE_CART = "Cart"
  TYPE_FAVORITE = "Favorite"
  TYPE_RESERVE = "Reserve"
  TYPE_SIMPLE = "Simple"


  LIST_TYPES = [TYPE_CART, TYPE_FAVORITE, TYPE_RESERVE, TYPE_SIMPLE]

  def self.search(user_id, search_user_email, search_name, types_list, page)
    items_per_page = count_record_per_page :wish_lists
    order = "users.email, wish_lists.name"

    conditions_clauses = []
    conditions_options = []

    if !user_id.nil?
      conditions_clauses << "wish_lists.user_id = ?"
      conditions_options << user_id
    end

    if !search_user_email.nil? && search_user_email != ""
      conditions_clauses << "upper(users.email) like upper(?)"
      conditions_options << "%#{search_user_email}%"
    end

    if !search_name.nil? && search_name != ""
      conditions_clauses << "upper(wish_lists.name) like upper(?)"
      conditions_options << "%#{search_name}%"
    end

    if !types_list.nil? && types_list != ""
      if types_list.is_a?(String)
        conditions_clauses << "type_list = ?"
        conditions_options << types_list
      end
      if types_list.is_a?(Array)
        list = nil
        types_list.each_index { |i| if list.nil? then list = "'#{types_list[i]}'" else list += ",'#{types_list[i]}'" end }

        conditions_clauses << "type_list in (#{list})"
      end
    end

    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
    if page.nil?
      @wish_lists = WishList.find :all, :include => :user, :conditions => conditions, :order => order
    else
      @wish_lists = WishList.paginate :per_page => items_per_page,:page => page, :include => :user, :conditions => conditions, :order => order
    end
    
    return @wish_lists
  end

  def self.has_count_attribute? wish_list_id
    wish_list = WishList.find(wish_list_id);
    wish_list.type_list == TYPE_CART || wish_list.type_list == TYPE_RESERVE
  end

  def self.is_cart? wish_list_id
    wish_list = WishList.find(wish_list_id);
    wish_list.type_list == TYPE_CART
  end

  def self.is_reserve? wish_list_id
    wish_list = WishList.find(wish_list_id);
    wish_list.type_list == TYPE_RESERVE
  end

  def self.save_material_to_wish_list wish_list_selected, material_selected
    error = false
    lbl_message = ''

    if !wish_list_selected.nil? && wish_list_selected != "-1"
      wish_list_id = wish_list_selected.to_i
    else
      error = true
      lbl_message = 'lbl_select_wish_list'
    end

    if !error
      if !material_selected.nil?
        materials_id = []
        if material_selected.is_a?(Array)
          material_selected.each { |material_id_str| materials_id << material_id_str.to_i }
          if materials_id.size == 0
            error = true
            lbl_message = 'lbl_must_select_materials'
          end
        else
          materials_id << material_selected.to_i
        end
      else
        error = true
        lbl_message = 'lbl_must_select_materials'
      end
    end

    if !error
      wish_list = WishList.find wish_list_id
      if wish_list.type_list == WishList::TYPE_CART
        #si la lista es el carro de compras
        session[:materials_id_to_add_cart] = materials_id
        redirect_to my_wish_list_define_materials_to_cart_from_catalog_path
      else
        #sino lo trato como una lista comun
        process_with_error = WishListsHelper.add_materials_to_wish_list(wish_list_id, materials_id)
        if process_with_error
          error = true
          lbl_message = 'lbl_material_add_wish_list_error'
        else
          lbl_message = 'lbl_material_add_wish_list_ok'
        end
      end
    end

    return error, lbl_message, wish_list_id
  end
end
